﻿using System;
using System.ComponentModel;
using System.Data;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.Task.Context
{
    public class ClientInstitution : Crud
    {
        #region Protected Backgroud Worker Function
        //protected void BgwGetTotalLateByEmployeeIdDoWork(object sender, DoWorkEventArgs e)
        //{
        //    var data = (Domain.ClientInstitution)e.Argument;
        //    //e.Result = new HRMO.Model.Table.ClientInstitution().GetTotalLateByEmployeeId(data.Employee.Id);
        //}

        //protected void BgwGetTotalLateByEmployeeIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    OnGetTotalLateByEmployeeId(this, new FormEvent(true) { Value = e.Result });
        //}

        //protected void BgwGetTotalUnderTimeByEmployeeIdDoWork(object sender, DoWorkEventArgs e)
        //{
        //    //var data = (Domain.ClientInstitution)e.Argument;
        //    //e.Result = new HRMO.Model.Table.ClientInstitution().GetTotalUnderTimeByEmployeeId(data.Employee.Id);
        //}

        //protected void BgwGetTotalUnderTimeByEmployeeIdWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        //{
        //    OnGetTotalUnderTimeByEmployeeId(this, new FormEvent(true) { Value = e.Result });
        //}
        #endregion

        #region Protected Overridable Function
        protected override void BgwFetchDoWork(object sender, DoWorkEventArgs e)
        {                                                                                                                                                                                                                                                                                                                                                                                                   
        }

        protected override void BgFetchRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        }

        protected override void BgwFetchListsDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (PagingLists)e.Argument;

            try
            {
                e.Result = new model.Table.ClientInstitution().GetClientInstitutionLists(a.Page, a.Size, a.FilterString);
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = new DataTable();
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }   
        }

        protected override void BgFetchListsRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerFetchLists(new FormEvent(!IsError) { Value = e.Result });
            IsError = false;
        }

        protected override void BgwInsertDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (DataRow)e.Argument;

            try
            {
                var clientId = ToUInt64(a["client_id"].ToString());
                var institutionId = ToUInt64(a["institution_id"].ToString());
                var department = a["department"].ToString();
                var isEmployed = Convert.ToBoolean(a["is_employed"].ToString());
                var position = a["position"].ToString();
                var salary = Convert.ToDecimal(a["salary"].ToString());
                var userCreateId = ToUInt64(a["user_create_id"].ToString());

                a["id"] = new model.Table.ClientInstitution().Insert(clientId, institutionId, userCreateId,
                                                                             department, isEmployed, position, salary);

                if (!ToUInt64(a["id"].ToString()).Equals(0))
                {
                    a["created_date"] = DateTime.Now;
                    a["is_active"] = true;
                }
                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                a["id"] = 0;
                e.Result = a;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgInsertRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            var a = (DataRow)e.Result;
            var id = ToUInt64(a["id"].ToString());
            TriggerInsert(new FormEvent(!id.Equals("0") && !IsError)
                              {
                                  Value = a,
                                  Exception = new Exception(id.Equals(0) ? "Insert failed." : "Insert success.")
                              });
            IsError = false;
        }

        protected override void BgwUpdateDoWork(object sender, DoWorkEventArgs e)
        {
            var a = (DataRow)e.Argument;
            try
            {
                var id = ToUInt64(a["id"].ToString());
                var clientId = ToUInt64(a["client_id"].ToString());
                var institutionId = ToUInt64(a["institution_id"].ToString());
                var department = a["department"].ToString();
                var isEmployed = Convert.ToBoolean(a["is_employed"].ToString());
                var position = a["position"].ToString();
                var salary = Convert.ToDecimal(a["salary"].ToString());
                var userCreateId = ToUInt64(a["user_create_id"].ToString());
                IsSuccess = new model.Table.ClientInstitution().Update(id, clientId, institutionId, userCreateId,
                                                                       department, isEmployed, position, salary);
                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = a;
                IsSuccess = false;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgUpdateRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerUpdate(new FormEvent(IsSuccess)
                              {
                                  Value = e.Result,
                                  Exception = new Exception(!IsSuccess ? "Update failed." : "Update success.")
                              });
        }

        protected override void BgwDeleteDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var a = (DataRow)e.Argument;
                IsSuccess = new model.Table.ClientInstitution().Delete(ToUInt64(a["id"].ToString()));
                e.Result = a;
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = 0;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }      
        }

        protected override void BgDeleteRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerDelete(new FormEvent(IsSuccess && !IsError)
                              {
                                  Value = e.Result,
                                  Exception = new Exception(!IsSuccess ? "Update failed." : "Update success.")
                              });
            IsError = false;
        }

        protected override void BgwToggleDoWork(object sender, DoWorkEventArgs e)
        {
        }

        protected override void BgToggleRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
        }

        protected override void BgwGetCountDoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                e.Result = new model.Table.ClientInstitution().GetCount();
            }
            catch (Exception ex)
            {
                // Error Logs here
                e.Result = 0;
                IsError = true;
                TriggerError(new FormEvent(true) { Exception = ex });
            }
        }

        protected override void BgwGetCountWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            TriggerGetCount(new FormEvent(!IsError) { Value = e.Result });
            IsError = false;
        }
        #endregion
    }
}
